package com.gzu.test;

import jakarta.servlet.ServletContextListener;
import jakarta.servlet.ServletRequestEvent;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
@WebListener("/hello-servlet")
public class RequestLogListener implements ServletContextListener {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
        long startTime = System.currentTimeMillis();
        //获取请求时间
        String requestTime = dateFormat.format(new Date());
       // 获取客户端IP
        String clientIp = request.getRemoteAddr();
        //获取请求方法
        String requestMethod = request.getMethod();
        //获取请求URL
        String requestUri = request.getRequestURI();
        //获取查询字符串
        String queryString = request.getQueryString();
        //获取User-Agent
        String userAgent = request.getHeader("User-Agent");

        // 将请求信息存储在请求属性中，以便在请求结束时获取
        request.setAttribute("startTime", startTime);
        request.setAttribute("requestTime", requestTime);
        request.setAttribute("clientIp", clientIp);
        request.setAttribute("requestMethod", requestMethod);
        request.setAttribute("requestUri", requestUri);
        request.setAttribute("queryString", queryString);
        request.setAttribute("userAgent", userAgent);
    }
    public void requestDestroyed(ServletRequestEvent sre) {
        //从ServletRequestEvent对象sre中获取当前的HttpServletRequest对象
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
        long startTime = (long) request.getAttribute("startTime");
        long endTime = System.currentTimeMillis();
        //计算请求处理时间
        long processingTime = endTime - startTime;

        String requestTime = (String) request.getAttribute("requestTime");
        String clientIp = (String) request.getAttribute("clientIp");
        String requestMethod = (String) request.getAttribute("requestMethod");
        String requestUri = (String) request.getAttribute("requestUri");
        String queryString = (String) request.getAttribute("queryString");
        String userAgent = (String) request.getAttribute("userAgent");

        // 使用日志框架记录请求信息，这里假设使用 SLF4J 和 Logback
        org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(RequestLogListener.class);
        logger.info("Request Time: {}, Client IP: {}, Request Method: {}, Request URI: {}, Query String: {}, User-Agent: {}, Processing Time: {} ms",
                requestTime, clientIp, requestMethod, requestUri, queryString, userAgent, processingTime);
        System.out.println("Request Time: " + requestTime + ", Client IP: " + clientIp + ", Request Method: " + requestMethod + ", Request URI: " + requestUri + ", Query String: " + queryString + ", User-Agent: " + userAgent + ", Processing Time: " + processingTime + " ms");
    }
}
